package com.danger.Presenter;

import android.content.Context;

import com.danger.Model.action.LoginModel;
import com.danger.Model.action.LoginModelImpl;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Model.listener.OnLoginListener;
import com.danger.Util.NetUtils;
import com.danger.View.LoginView;

/**
 * Created by danger on 2016/3/17.
 */
public class LoginPresenterImpl implements LoginPresenter {
    private LoginView loginView;
    private LoginModel loginModel;
    private Context context;

    public LoginPresenterImpl(Context context, LoginView loginView) {
        this.context = context;
        this.loginView = loginView;
        loginModel = new LoginModelImpl(context);
    }

    @Override
    public void login(String username,String password) {
        String msg = loginModel.check(username,password);
        if (msg.equals("正确")) {
            loginView.showDialog();
            loginModel.login(username,password, new OnLoginListener() {
                @Override
                public void onSuccess(UserInfoBean user) {
                    loginView.hideDialog();
                    loginView.showSuccess();
                    loginView.toIndex();
                }

                @Override
                public void onError(String msg) {
                    loginView.hideDialog();
                    if (NetUtils.isConnected(context)) {
                        loginView.showError(msg);
                    } else {
                        loginView.showError("无连接，请检查网络设置");
                    }
                }
            });
        } else {
            loginView.showError(msg);
        }
    }
}
